494A - Treasure - CodeForces Solution


greedy *1500

Please click on ads to support us..

Python Code:

def solve(s):
    cnt, d, mid = 0, 0, len(s)
    for ch in s:
        if ch == '(':
            d += 1
            continue
        d -= 1
        if d < 0:
            print('-1')
            return
        if ch == '#':
            cnt += 1
            mid = d
        elif mid > d:
            mid = d
    if mid < d:
        print('-1')
    else:
        for i in range(cnt - 1): print(1)
        print(d + 1)
s = input()
solve(s)

C++ Code:

#include <bits/stdc++.h>
#include <math.h>
//in the name of god,aka allah
//**shine till grow**
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define pi pair<long long , long long>
#define pii pair<long long , pair<long long , long long>>
const int maxm = 5e5 + 3;
const long long mod = 998244353;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
ll l,r,mid;
ll n,m;
ll dis[maxm] , sum[maxm];
bool isval(int mid){
    //cout << mid <<" " << mid*mid-mid <<endl;
    if (((mid-1)*mid)/2 < m) return 0;
    return 1;
}
ll darage[maxm] , ss , mm;
queue<int> q;
vector<ll> g[maxm] , z[maxm];
ll sath[maxm];
bool vis[maxm] , gos[maxm];
ll pedaret[maxm];
ll get_par(ll v){
    if (pedaret[v]==v) return v;
    return pedaret[v] = get_par(pedaret[v]);
}
void merge(ll r , ll q){
    r = get_par(r) , q = get_par(q);
    if (r!=q){
        if (sath[r]<sath[q]) swap(r,q);
        pedaret[q] = r;
        sath[r] += sath[q];
    }
    return ;
}
ll pars1[maxm] , pars2[maxm];
vector<ll> se[maxm];
set<ll> st[maxm];
ll rp[maxm];
pi w[maxm];
ll dp[maxm];
//ll rw[maxm][maxm];
map<ll,ll> mp;
bool ch(string s){
    ll ans = 0 , r = 0;
    for (int i=0; i<n; i++){
        if (s[i]==')') ans--;
        else ans++;
        if (ans<0) return 0;
    }
    return ans==0;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    string s;
    cin >>s;
    n = s.size();
    for (int i=0; i<n; i++){
        if (s[i]=='#') r = i+1;
    }
    if (!r) return cout<<(ch(s)?"YES":"NO"),0;
    vector<int> vec;
    for (int i=0; i<n; i++){
        if (s[i]=='#' && i+1 != r) s[i] = ')' , vec.pb(1);
    }
    r = 0;
    for (int i=0; i<n; i++){
        if (s[i]=='#'){r=i+1;break;}
        else{
            if (s[i]==')') ss--;
            else ss++;
            if (ss<0) return cout<<"-1",0;
        }
    }
    for (int i=n-1; i>=r; i--){
        if (s[i]=='(') mm--;
        else mm++;
        if (mm<0) return cout<<-1,0;
    }
    if (mm<0 || mm>=ss) return cout<<-1,0;
    else vec.pb(ss-mm);
    for (auto x : vec) cout<<x<<endl;
}


Comments

Submit
0 Comments
More Questions

1301C - Ayoub's function
38E - Let's Go Rolling
171G - Mysterious numbers - 2
1183C - Computer Game
400C - Inna and Huge Candy Matrix
417A - Elimination
222A - Shooshuns and Sequence
1736A - Make A Equal to B
1736B - Playing with GCD
887C - Solution for Cube
1737C - Ela and Crickets
1741C - Minimize the Thickness
1741A - Compare T-Shirt Sizes
1741D - Masha and a Beautiful Tree
109B - Lucky Probability
1741B - Funny Permutation
1741E - Sending a Sequence Over the Network
344B - Simple Molecules
370A - Rook Bishop and King
546E - Soldier and Traveling
1741G - Kirill and Company
1200B - Block Adventure
1088B - Ehab and subtraction
1270B - Interesting Subarray
478C - Table Decorations
1304C - Air Conditioner
1311C - Perform the Combo
1519C - Berland Regional
361A - Levko and Table
5E - Bindian Signalizing